package com.kuaiditu.assistant.module.retrofit.impl;

import com.alibaba.fastjson.JSON;
import com.kuaiditu.assistant.api.yd_api.YDBaseRequest;
import com.kuaiditu.assistant.api.yd_api.YDBaseResponse;
import com.kuaiditu.assistant.module.retrofit.ConverterAdapter;
import com.kuaiditu.assistant.util.LogUtil;

import java.io.IOException;

/**
 * Created by Eenie on 2017/9/7.
 * Email: eeniework@gmail.com
 * Des:
 */

public class YDConverterAdapter implements ConverterAdapter<YDBaseRequest,YDBaseResponse> {


    @Override
    public String onConverterRequest(YDBaseRequest request) throws IOException {
        return request.getReqString();
    }

    @Override
    public String onConverterResponse(YDBaseResponse response) throws IOException {

        LogUtil.e(response.getDecodeBody());

        if (response.getSuccess()) {
            if (response.getDeEntity().containsKey("result")) {

                if (response.getDeEntity().getBoolean("result")) {
                    return JSON.toJSONString(response);
                } else {
                    throw new IOException(response.getDeEntity().getString("msg"));
                }

            } else {
                return JSON.toJSONString(response);
            }

        } else {
            throw new IOException(response.getErrorcode());
        }

    }

    @Override
    public YDBaseResponse strToObj(String str) {
        return JSON.parseObject(str, YDBaseResponse.class);
    }
}
